//
//  DataSummaryBloodOxygenCell.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit
import Charts

class DataSummaryBloodOxygenCell: DataSummaryCell {
    
    @IBOutlet var valueLabel: UILabel!
    @IBOutlet var chartView: LineChartView!
    
    // 血氧
    var dataSet: LineChartDataSet {
        // TODO: 准确数据
        let entries = (0...24)
            .map({ $0 })
            .filter({ _ in Bool.random() })
            .map { i in
                ChartDataEntry(x: Double(i), y: Double((0...100).randomElement()!))
            }
        let dataSet = LineChartDataSet(entries: entries)
        setupDataSet(dataSet)
        return dataSet
    }
    
    var dataSets: [LineChartDataSet] {[
        dataSet
    ]}
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setupChart()
    }
    
    override func update(vm: DataSummaryCellViewModel, data: Any?) {
        super.update(vm: vm, data: data)
        drawChart()
        
        guard let boData = (data as? SummaryDataModel)?.bloodOxygen else {
            showEmptyData()
            return
        }
        
        valueLabel.text = "\(boData.bloodOxygen)"
    }
    
    func drawChart() {
        let data = LineChartData(dataSets: dataSets)
        chartView.data = data
        chartView.animate(xAxisDuration: 1)
    }
    
    func setupChart() {
        chartView.isUserInteractionEnabled = false
        
//        chartView.gridBackgroundColor
//        chartView.drawGridBackgroundEnabled
        
//        chartView.borderColor
//        chartView.borderLineWidth
//        chartView.drawBordersEnabled
        
//        chartView.clipValuesToContentEnabled
//        chartView.clipDataToContentEnabled
        
//        chartView.minOffset
//        chartView.keepPositionOnRotation
        chartView.leftAxis.enabled = false
        chartView.rightAxis.enabled = false
//        chartView.leftYAxisRenderer
//        chartView.rightYAxisRenderer
        chartView.xAxis.enabled = false
//        chartView.xAxisRenderer
//        chartView.setVisibleXRange(minXRange: 0, maxXRange: 24)
//        chartView.setVisibleYRange(minYRange: 0, maxYRange: 100, axis: .left)
        
        // 用户交互
//        chartView.dragEnabled
//        chartView.dragXEnabled
//        chartView.dragYEnabled
//        chartView.setScaleEnabled(false)
//        chartView.scaleXEnabled
//        chartView.scaleYEnabled
//        chartView.doubleTapToZoomEnabled
//        chartView.highlightPerDragEnabled
//        chartView.pinchZoomEnabled
//        chartView.autoScaleMinMaxEnabled
//        chartView.dragDecelerationEnabled
//        chartView.dragDecelerationFrictionCoef
        
        // 绘制
//        chartView.maxVisibleCount
        
        // 图例
        chartView.legend.enabled = false
//        chartView.legendRenderer
        
        // 空数据
        chartView.noDataText = "没有数据"
//        chartView.noDataFont
//        chartView.noDataTextColor
//        chartView.noDataTextAlignment
        
        // 其他
//        chartView.highlighter
//        chartView.highlightPerTapEnabled
//        chartView.renderer
//        chartView.viewPortHandler
//        chartView.chartAnimator
        
        // Marker
        chartView.drawMarkers = false
//        chartView.marker
        
        // 位置
//        chartView.extraTopOffset
//        chartView.extraRightOffset
//        chartView.extraBottomOffset
//        chartView.extraLeftOffset
    }
    
    func setupDataSet(_ set: LineChartDataSet) {
//        set.visible
        
        // 线的设置
//        set.isDrawLineWithGradientEnabled = false
//        set.gradientPositions
//        set.lineDashPhase
//        set.lineDashLengths
//        set.lineCapType = .butt
        set.lineWidth = 2
        
        // 数据的点
        set.circleRadius = 2
        set.circleHoleRadius = 1
        set.setCircleColor(.white)
        set.drawCirclesEnabled = true
        set.circleHoleColor = HEX(0xEAAD3C)
        set.drawCircleHoleEnabled = true
//        set.drawIconsEnabled = true
//        set.iconsOffset
        
        // 填充
//        set.fillFormatter
//        set.fillColor
//        set.fill
//        set.fillAlpha
        set.drawFilledEnabled = true
        let gradientColors = [
            UIColor(hex: 0xEAAD3C, alpha: 0.58)!.cgColor,
            UIColor(hex: 0xFFA700, alpha: 0)!.cgColor
        ]
        let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!
        set.fill = LinearGradientFill(gradient: gradient, angle: -90)
        
        // 指示器
//        set.drawHorizontalHighlightIndicatorEnabled
//        set.drawVerticalHighlightIndicatorEnabled
//        set.setDrawHighlightIndicators(false)
        
        // 高亮
//        set.highlightColor
//        set.highlightLineWidth
//        set.highlightLineDashPhase
//        set.highlightLineDashLengths
//        set.highlightEnabled
        
        // 其他
        set.setColor(HEX(0xEAAD3C)!)
//        set.colors
//        set.valueColors
//        set.axisDependency
//        set.valueTextColor
//        set.valueFont
//        set.valueLabelAngle
//        set.valueFormatter
        set.drawValuesEnabled = false
        
        // form
        set.form = .none
//        set.formSize
//        set.formLineWidth
//        set.formLineDashPhase
//        set.formLineDashLengths
    }
}
